#include <vector>
#include <string>
#include <algorithm>

// there is a better cpp solution

class Solution
{
public:
	int lengthOfLongestSubstring(std::string s)
	{
		std::vector<int> alpha(256, -1);
		int maxLength = 0;
		for (int i = 0; i < s.size(); i++) {
			auto c = s[i];
			if (alpha[c] != -1) {
				int length = 0;
				int oldIndex = alpha[c];
				for (auto& c : alpha) {
					if (c != -1) length++;
					if (c <= oldIndex) c = -1;
				}
				maxLength = std::max(maxLength, length);
			}
			alpha[c] = i;
		}
		int length = 0;
		for (int j = 0; j < 256; j++) if (alpha[j] != -1) length++;
		maxLength = std::max(maxLength, length);
		return maxLength;
	}
};